import 'package:riverpod/riverpod.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

import '../../../auth/data/models/user.dart';
import '../../data/repositories/profile_repository.dart';

part 'profile_controller.g.dart';

final profileRepositoryProvider = Provider<ProfileRepository>((ref) {
  return ProfileRepository();
});

@riverpod
class ProfileController extends _$ProfileController {
  @override
  Future<User?> build() async {
    return await fetchUserProfile(); //直接返回 AsyncValue
  }

  Future<User?> fetchUserProfile() async {
    final repository = ref.watch(profileRepositoryProvider);
    final user = await repository.getUser();
    return user;
  }

  Future<void> logOut() async {
    final repository = ref.watch(profileRepositoryProvider);
    await repository.logOut();
    // state = const User(uid: 'uid'); // Reset state after logout
  }
}
